

import HeadPrefab from "../../../component/HeadPrefab";
import { Base64Helper } from "../../../engine/Base64Helper";
import ComponentBase from "../../../engine/ComponentBase";
import DateUtil from "../../../engine/DateUtil";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import RedDotManager from "../../manager/RedDotManager";
import { ClubModel } from "../../model/ClubModel";
import { ClubShenheModel } from "../../model/ClubShenheModel";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubShenHe extends ComponentBase {

    private defaultItemNode: cc.Node
    private dataList: Array<JhaoProto.IClub_User> = [];
    private txtPage: cc.Label;
    private page: number = 1
    private totalPage: number = 1
    private pageSize: number = 3

    onEnable(): void {
        super.onEnable();
        this.defaultItemNode = this.findChild(this.node, "scrollView/view/content/itemNode")
        this.defaultItemNode.active = false;
        ClubSocketClient.req_club_apply_list(UserModel.instance.nowClub.clubid, (users: Array<JhaoProto.IClub_User>) => {
            this.refresh()
        })
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.page > 1) {
                this.page--
                this.refresh()
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.page < this.totalPage) {
                this.page++
                this.refresh()
            }
        })
        this.txtPage = this.getChild(this.node, "txtPage", cc.Label)
        this.refresh()
    }

    private refresh() {
        this.dataList = ClubShenheModel.modelList;
        this.totalPage = this.dataList.length % this.pageSize == 0 ? this.dataList.length / this.pageSize : Math.floor(this.dataList.length / this.pageSize) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = this.page + "/" + this.totalPage;

        this.unscheduleAllCallbacks()
        this.hideAllChildren(this.defaultItemNode.parent)
        // for (let i = 0; i < this.dataList.length; i++) {
        for (let i = 0; i < this.pageSize; i++) {
            let realIndex: number = (this.page - 1) * this.pageSize + i
            let dataObj: JhaoProto.IClub_User = this.dataList[realIndex];
            if (dataObj) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    this.scheduleOnce(() => {
                        this.initOne(dataObj, i)
                    }, 0.02 * i)
                } else {
                    item.active = true;
                    this.initOne(dataObj, i)
                }
            }
        }
    }


    private initOne(dataObj: JhaoProto.IClub_User, i: number) {
        let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
        if (!itemNode) {
            itemNode = cc.instantiate(this.defaultItemNode)
            itemNode.parent = this.defaultItemNode.parent;
        }
        itemNode.active = true;
        let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
        head.setHead(dataObj.headimgurl);
        let txtName: cc.Label = this.getChild(itemNode, "txtName", cc.Label)
        txtName.string = dataObj.nickName
        let txtId: cc.Label = this.getChild(itemNode, "txtId", cc.Label)
        txtId.string = dataObj.userid + ""
        let txtApplyTime: cc.Label = this.getChild(itemNode, "txtApplyTime", cc.Label)
        txtApplyTime.string = "申请时间:" + DateUtil.formatDateStr(dataObj.joinTime)
        // let txtVip: cc.Label = this.getChild(itemNode, "txtVip", cc.Label)
        // if (dataObj["invite"]) {
        //     txtVip.string = dataObj["invite"] + ""
        // } else {
        //     txtVip.string = ""
        // }
        let btndisagree: cc.Node = this.findChild(itemNode, "btndisagree")
        UIHelper.onAddEvent(btndisagree, () => {
            ClubSocketClient.req_club_apply_handle(dataObj.id, false, dataObj.clubid, dataObj.userid, () => {
                this.dataList.splice(i, 1)
                RedDotManager.instance.checkShowReddotList([RedDotManager.RedDot_Type.Club_shenhe, RedDotManager.RedDot_Type.Club_shenhe2])
                this.refresh()
            })
        })
        let btnagree: cc.Node = this.findChild(itemNode, "btnagree")
        UIHelper.onAddEvent(btnagree, () => {
            ClubSocketClient.req_club_apply_handle(dataObj.id, true, dataObj.clubid, dataObj.userid, () => {
                this.dataList.splice(i, 1)
                RedDotManager.instance.checkShowReddotList([RedDotManager.RedDot_Type.Club_shenhe, RedDotManager.RedDot_Type.Club_shenhe2])
                this.refresh()
            })
        })

    }

}
